package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerMyExtensionFansFragmentComponent;
import com.wgts.wgts.dagger.module.MyExtensionFansFragmentModule;
import com.wgts.wgts.localbean.LocalShopKeepFansEntity;
import com.wgts.wgts.mvp.presenter.IMyExtensionFansFragmentpersenter;
import com.wgts.wgts.mvp.view.IMyExtensionFansFragmentView;
import com.wgts.wgts.netmodle.resp.FansSumEntity;
import com.wgts.wgts.netmodle.resp.ShopKeepFansEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyExtensionFansFragment extends LazyBaseFragment implements IMyExtensionFansFragmentView {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;


    private static final Double THOUSAND = 10000d;

    private static final String LEVL = "levl";

    private static final String USER_ID = "userId";
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.tv_all_util)
    TextView tvAllUtil;
    @BindView(R.id.tv_shop_keeper)
    TextView tvShopKeeper;
    @BindView(R.id.tv_shop_keeper_util)
    TextView tvShopKeeperUtil;
    @BindView(R.id.tv_nomal_user)
    TextView tvNomalUser;
    @BindView(R.id.tv_nomal_user_util)
    TextView tvNomalUserUtil;


    private int levl;

    private int userId;

    private static final String FOR_OWN_LEVEL = "FOR_OWN_LEVEL";

    private int ownLevel;

    private boolean haMore;

    @Inject
    IMyExtensionFansFragmentpersenter persenter;

    LinearLayoutManager linearLayoutManager;

    private ArrayList<ShopKeepFansEntity.Entity> arrayList;

    private MyExtensionAdapter adapter;

    private MyExtensionFansFragmentToActivity myExtensionFansFragmentToActivity;

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        myExtensionFansFragmentToActivity = (MyExtensionFansFragmentToActivity)context;
    }

    @Override
    protected void loadData() {

        levl = getArguments().getInt(LEVL);

        userId = getArguments().getInt(USER_ID);


        persenter.initFans(levl, userId);

        persenter.initFansSum(levl, userId);


    }

    @Override
    public void init() {
        DaggerMyExtensionFansFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .myExtensionFansFragmentModule(new MyExtensionFansFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_extension_fans_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {

        ownLevel = getArguments().getInt(FOR_OWN_LEVEL);

        arrayList = new ArrayList<>();
        linearLayoutManager = new LinearLayoutManager(getContext());
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new MyExtensionAdapter(getContext(), arrayList);
        recycleView.setAdapter(adapter);
        adapter.setOnClickListener(new MyExtensionAdapter.OnClickListener() {
            @Override
            public void onClick(int i) {
                LocalShopKeepFansEntity localShopKeepFansEntity = new LocalShopKeepFansEntity();

                localShopKeepFansEntity.relationLevel = arrayList.get(i).relationLevel;
                localShopKeepFansEntity.uesrId = arrayList.get(i).userId;
                localShopKeepFansEntity.userImage = arrayList.get(i).userImage;
                localShopKeepFansEntity.userNickName = arrayList.get(i).userNickName;
                localShopKeepFansEntity.userRole = arrayList.get(i).userRole;
                localShopKeepFansEntity.userTime = arrayList.get(i).userTime;
                localShopKeepFansEntity.userPhone = arrayList.get(i).userPhone;
                Intent intent = new Intent(getContext(), MyExtensionFansActivity.class);
                intent.putExtra(MyExtensionFansActivity.USER_INFO, localShopKeepFansEntity);
                if(ownLevel == 2){
                    intent.putExtra(MyExtensionFansActivity.FANS_STATE, 2);
                }else if(ownLevel == 3){
                    intent.putExtra(MyExtensionFansActivity.FANS_STATE, 3);
                }
                startActivity(intent);
            }
        });

//        smartRefreshLayout.setEnableLoadmore(true);
//        smartRefreshLayout.setEnableRefresh(false);
//        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
//            @Override
//            public void onLoadmore(RefreshLayout refreshlayout) {
//                persenter.loadMore();
//            }
//        });
    }


    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void fansResult(ShopKeepFansEntity entity, boolean hasMore) {

        this.haMore = hasMore;

//        if (hasMore) {
//            smartRefreshLayout.setEnableLoadmore(false);
//        }else{
//            smartRefreshLayout.setEnableLoadmore(true);
//        }

        arrayList.addAll(entity.list);
        adapter.setNoMore(hasMore);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void fansSum(FansSumEntity entity) {

        DecimalFormat df = new DecimalFormat("#.##");

        if (Double.valueOf(entity.total) / THOUSAND <  1) {
            tvAllUtil.setVisibility(View.INVISIBLE);
            tvAll.setText(String.valueOf(entity.total));
        } else {
            tvAllUtil.setVisibility(View.VISIBLE);
            tvAll.setText(df.format(Double.valueOf(entity.total) / THOUSAND));
        }


        if (Double.valueOf(entity.shopkeeper) / THOUSAND <  1) {
            tvShopKeeperUtil.setVisibility(View.INVISIBLE);
            tvShopKeeper.setText(String.valueOf(entity.shopkeeper));
        } else {
            tvShopKeeperUtil.setVisibility(View.VISIBLE);
            tvShopKeeper.setText(df.format(Double.valueOf(entity.shopkeeper) / THOUSAND));
        }


        if (Double.valueOf(entity.normal) / THOUSAND <  1) {
            tvNomalUserUtil.setVisibility(View.INVISIBLE);
            tvNomalUser.setText(String.valueOf(entity.normal));
        } else {
            tvNomalUserUtil.setVisibility(View.VISIBLE);
            tvNomalUser.setText(df.format(Double.valueOf(entity.normal) / THOUSAND));
        }

    }

    @Override
    public void loadFansResult(ShopKeepFansEntity entity, boolean hasMore) {

        this.haMore = hasMore;

//        if (hasMore) {
//            smartRefreshLayout.setEnableLoadmore(false);
//        }else{
//            smartRefreshLayout.setEnableLoadmore(true);
//        }
//        smartRefreshLayout.finishLoadmore();
        if(myExtensionFansFragmentToActivity != null){
            myExtensionFansFragmentToActivity.onLoadFinish();
        }

        arrayList.addAll(entity.list);
        adapter.setNoMore(hasMore);
        adapter.notifyDataSetChanged();
    }

    public static Fragment getInstance(int levl, int userId, int ownLevel) {
        MyExtensionFansFragment fragment = new MyExtensionFansFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(LEVL, levl);
        bundle.putInt(USER_ID, userId);
        bundle.putInt(FOR_OWN_LEVEL, ownLevel);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void loadMore(){
        if(!haMore){
            persenter.loadMore();
        }
        if(myExtensionFansFragmentToActivity != null){
            myExtensionFansFragmentToActivity.onLoadFinish();
        }
    }

    public interface MyExtensionFansFragmentToActivity{
        void onLoadFinish();
    }



}
